import json, copy
from tools import conf
import xlrd
from mapping.dataNode import dataNode

catagforyear = 1
catagforquarter = 2


indexList = [dict() for i in range(3)]
parentNode = dataNode(None, 0)

def loadsheet(name):
    workbook = xlrd.open_workbook(conf.filepath)
    sheet_object = workbook.sheet_by_name(name)
    return sheet_object

def insertNode(name, itera, row):
    def insert(myNode, level):
        value = itera.cell_value(row, conf.automated_values[name]["colflag"][level])
        if myNode.version:
            return myNode.updateNode([int(itera.cell_value(row, i)) for i in conf.automated_values[name]["colflag"][6:]], 1)
        if not myNode.son.get(value):
            Node = dataNode(value, len(conf.automated_values[name]["colflag"])-6,
            start = itera.cell_value(row, conf.automated_values[name]["colflag"][4]), 
            end = itera.cell_value(row, conf.automated_values[name]["colflag"][5]))
            myNode.son[value] = Node
            if level == 2:
                Node.version = itera.cell_value(row, conf.automated_values[name]["colflag"][3])
            return myNode.updateNode(list(insert(Node, level+1)), 4)
        else:
            if level == 2:
                if myNode.son.get(value).version < itera.cell_value(row, conf.automated_values[name]["colflag"][3]):
                    myNode.son.get(value).version = itera.cell_value(row, conf.automated_values[name]["colflag"][3])
                    return myNode.updateNode(list(myNode.son.get(value).updateNode(
                        [int(itera.cell_value(row, i)) for i in conf.automated_values[name]["colflag"][6:]], 2)), 3)
            if myNode.son.get(value).changeV:
                myNode.son.get(value).changeV = False
                return myNode.updateNode(list(insert(myNode.son.get(value), level+1)), 3)
            return myNode.updateNode(list(insert(myNode.son.get(value), level+1)), 4)
    insert(parentNode, 0)

def parseData(name):
    sheet_object = loadsheet(name)
    nrows = sheet_object.nrows
    for row in range(1, nrows):
        insertNode(name, sheet_object, row)

def PieData(index):
    x_data = [[],[],[]]
    y_data = [[],[],[]]
    for son_one in parentNode.son.keys():
        x_data[0].append(son_one)
        y_data[0].append(parentNode.son[son_one].caseDetail[index])
        for son_two in parentNode.son[son_one].son.keys():
            x_data[1].append(son_two)
            y_data[1].append(parentNode.son[son_one].son[son_two].caseDetail[index])
            for son_three in parentNode.son[son_one].son[son_two].son.keys():
               x_data[2].append(son_three) 
               y_data[2].append(parentNode.son[son_one].son[son_two].son[son_three].caseDetail[index])
    res = []
    for i in range(3):
        res.append([list(z) for z in zip(x_data[i], y_data[i])])
    print(res)
    return res

def PieDataforjson(index, name):
    retData = dict()
    innerData = dict()
    retData['data'] = list()
    for son_one in parentNode.son.keys():
        innerData['name'] = son_one
        innerData['value'] = parentNode.son[son_one].caseDetail[index]
        innerData['children'] = list()
        innerDatatwo = dict()
        for son_two in parentNode.son[son_one].son.keys():
            innerDatatwo['name'] = son_two
            innerDatatwo['value'] = parentNode.son[son_one].son[son_two].caseDetail[index]
            innerDatatwo['children'] = list()
            innerDatathree= dict()
            for son_three in parentNode.son[son_one].son[son_two].son.keys():
                innerDatathree['name'] = son_three 
                innerDatathree['value'] = parentNode.son[son_one].son[son_two].son[son_three].caseDetail[index]
                innerDatatwo['children'].append(copy.deepcopy(innerDatathree))
            innerData['children'].append(copy.deepcopy(innerDatatwo))
        retData['data'].append(copy.deepcopy(innerData))

    with open('./jsons/%s.json'%name, mode='w', encoding='utf-8') as fp:
        json.dump(retData, fp, ensure_ascii=False)


def BarData(start = None, end = None, catag = catagforyear):
    if catag == catagforyear: #年度分类 时间问题有个bug, 最新的需要更新日期
        dataforBar = {str(year): [0]*7 for year in range(int(start[:4]),  int(end[:4])+1)}
    else:
        #TODO 季度分类
        pass

    for son_one in parentNode.son.keys():
        for son_two in parentNode.son[son_one].son.keys():
            for son_three in parentNode.son[son_one].son[son_two].son.keys():
                for i, _ in enumerate(parentNode.son[son_one].son[son_two].son[son_three].caseDetail):
                    dataforBar[str(parentNode.son[son_one].son[son_two].son[son_three].end)[:4]][i] += _
                
    return dataforBar


if __name__ == "__main__":
    for name in conf.automated_values.keys():
        parseData(name)
        # Pies = {
        #     0: "用例总数",
        #     1: "用例执行成功数",
        #     2: "用例执行失败数",
        #     3: "用例执行总数",
        #     4: "总测试轮次",
        #     5: "提示缺陷总数",
        #     6: "缺陷总数 "
        # }

        for x, y in enumerate(conf.automated_values[name]["colname"]):
            PieDataforjson(x, y)
            # Pie.myPie(PieData(x), y)

    # Bar.myBar("%s.html"%name, conf.automated_values[name]["colname"], BarData(start="2020", end="2021", catag=catagforyear))